<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
      xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head th:include="include :: header"></head>
<body class="hold-transition skin-blue sidebar-mini">

<!--创建字典数据模态框-->
<div class="modal fade" data-backdrop="static" data-keyboard="false" id="modal-create-dict">
    <div class="modal-dialog" style="width:500px">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title">设备名称型号字典数据信息</h4>
            </div>
            <div class="modal-body">
                <form id="dictForm" class="form-horizontal">
                    <input type="hidden" id="id" name="id">
                    <div class="form-group">
                        <label class="col-sm-4 control-label" for="dictForm-name">设备名称型号</label>
                        <div class="col-sm-6">
                            <input id="dictForm-name" type="text" class="form-control" name="name" placeholder="设备名称型号">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-4 control-label" for="dictForm-orderNo">排序</label>
                        <div class="col-sm-6">
                            <input id="dictForm-orderNo" type="number" min="0" class="form-control" name="orderNo" placeholder="展示排序">
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" shiro:haspermission="basic:dict:edit" class="btn btn-info"
                        id="btn-create-dict">提交
                </button>
                <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
            </div>
        </div>
    </div>
</div>

<section class="content-header">
    <h1>设备名称型号数据列表</h1>
</section>
<section class="content">
    <div class="row">
        <div class="col-xs-12">
            <div class="box">
                <div class="box-header">
                    <form id="queryForm" onsubmit="return false;">
                        <div class="row">
                            <div class="form-inline">
                                <div class="form-group">
                                    <label for="query-name">设备型号</label>
                                    <input id="query-name" type="text" name="name" class="form-control"
                                           placeholder="请输入名称">
                                </div>
                                <button type="button" class="btn btn-success" id="btn-search"
                                        onclick="js.table.search(table)">
                                    <span class="Bold">搜索</span>
                                </button>
                                <a href="javascript:void(0);"
                                   class="btn btn-info pull-right" onclick="openCreateDictModal()">
                                    <span class="Bold">添加</span>
                                </a>
                            </div>
                        </div>
                    </form>
                </div>
                <div class="box-body">
                    <table id="bootstrap-table"></table>
                </div>
            </div>
        </div>
    </div>
</section>
<div th:include="include :: footer"></div>
<script th:inline="javascript">

    let table;

    /** 打开创建数据模态框 */
    function openCreateDictModal(pid) {
        if (pid) {
            $("#pid").val(pid);
        } else {
            $("#pid").val('');
        }
        js.modal.open("modal-create-dict");
    }

    /** 打开编辑数据模态框 */
    function openEditDictModal(result) {
        $("#dictForm").fillData(result);
        js.modal.open("modal-create-dict");
        setTimeout(function () {
            js.validate.validing("dictForm");
        }, 500);
    }

    /** 删除数据 */
    function delDict(id) {
        js.modal.confirm("确认删除吗？", function () {
            js.post({
                url: ctx + "craft/dict/equ/del",
                data: {id: id},
                success: function (result) {
                    if (result.type === web_status.SUCCESS) {
                        js.modal.success(result.msg);
                        js.table.refresh(table);
                    } else {
                        js.modal.warning(result.msg);
                    }
                }
            })
        })
    }

    $(function () {

        /** 加载列表 */
        table = js.table.init({
            url: ctx + "craft/dict/equ/list",
            columns: [
                {
                    title: '序号', field: 'id', width: '50',
                    formatter: function (value, row, index) {
                        return index + 1;
                    }
                },
                {title: "设备类型名称", field: 'name'},
                {title: "排序", field: 'orderNo'},
                {
                    title: "状态", field: 'isDel',
                    formatter: function (value, row) {
                        if (row.isDel === 0) {
                            return '<span class="badge label-success">正常</span>';
                        } else if (row.isDel === 1) {
                            return '<span class="badge label-danger">删除</span>';
                        }
                    }
                },
                {
                    title: "操作",
                    width: '250',
                    formatter: function (value, row) {
                        let actions = [];
                        if (hasP('basic:dict:edit')) {
                            actions.push('<a class="btn btn-warning btn-xs" href="javascript:void(0)" onclick="openEditDictModal(' + js.jsonStringify(row) + ')"> 编辑</a> ');
                            actions.push('<a class="btn btn-danger btn-xs" href="javascript:void(0)" onclick="delDict(' + row.id + ')"> 删除</a>');
                        }
                        return actions.join('');
                    }
                }
            ]
        });

        /** 初始化验证 */
        js.validate.init("dictForm", {
            fields: {
                name: {
                    validators: {
                        notEmpty: {message: '请输入名称'}
                    }
                }
            }
        });

        $("#btn-create-dict").click(function () {
            debugger;
            let url = ctx + "craft/dict/equ/save";
            if ($("#id").val()) {
                url = ctx + "craft/dict/equ/update";
            }
            js.validSubmit({
                formId: "dictForm",
                url: url,
                data: new FormData($("#dictForm")[0]),
                success: function (result) {
                    if (result.type === web_status.SUCCESS) {
                        js.modal.success(result.msg);
                        js.table.refresh(table);
                        js.modal.hide("modal-create-dict");
                    } else {
                        js.modal.warning(result.msg);
                    }
                }
            });
        })

        /** 隐藏 modal-create-dict */
        js.modal.hideEvent("modal-create-dict", function () {
            js.validate.reset("dictForm");
            js.reset("dictForm");
        })
    });
</script>
</body>
</html>
